package com.spider.silence.main;

import com.spider.silence.output.MongoWriter;
import com.spider.silence.utils.FtpUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.*;
/**
 * 读取文件 入库mongodb
 * Created by qiuhao on 2017/2/10.
 */
public class FTPMain {
    public static FTPClient ftpClient =null;
    static {
        ftpClient = FtpUtil.getClient();
    }

    public static void main(String[] args) throws  Exception{

            ftpClient = FtpUtil.getClient();
            int a = 0;
            String path = "";
            FTPFile[] ftpFiles = ftpClient.listFiles("/");
            FTPFile[] ftpFiles1 = null;
            FTPFile[] ftpFiles2 = null;
            FTPFile[] ftpFiles3 = null;
            FTPFile[] ftpFiles4 = null;
            FTPFile[] ftpFiles5 = null;
            FTPFile[] ftpFiles6 = null;
            if (ftpFiles.length > 0) {
                for (int i = 0; i < ftpFiles.length; i++) {
                    ftpClient = FtpUtil.getClient();
                    path = "/" + ftpFiles[i].getName() + "/";
                    ftpFiles1 = ftpClient.listFiles(path);
                    ftpClient.disconnect();
                    if (ftpFiles1.length > 0) {
                        for (int j = 0; j < ftpFiles1.length; j++) {
                            ftpClient = FtpUtil.getClient();
                            path = "/" + ftpFiles[i].getName() + "/" + ftpFiles1[j].getName() + "/";
                            ftpFiles2 = ftpClient.listFiles(path);
                            ftpClient.disconnect();
                            if (ftpFiles2.length > 0) {
                                for (int k = 0; k < ftpFiles2.length; k++) {
                                    ftpClient = FtpUtil.getClient();
                                    path = "/" + ftpFiles[i].getName() + "/" + ftpFiles1[j].getName() + "/" + ftpFiles2[k].getName() + "/";
                                    ftpFiles3 = ftpClient.listFiles(path);
                                    ftpClient.disconnect();
                                    if (ftpFiles3.length > 0) {
                                        for (int l = 0; l < ftpFiles3.length; l++) {
                                            ftpClient = FtpUtil.getClient();
                                            path = "/" + ftpFiles[i].getName() + "/" + ftpFiles1[j].getName() + "/" + ftpFiles2[k].getName() + "/" + ftpFiles3[l].getName() + "/";
//                                        ftpClient.changeWorkingDirectory("/");
//                                        ftpClient.enterLocalActiveMode();
//                                        ftpClient.setRemoteVerificationEnabled(false);
                                            ftpFiles4 = ftpClient.listFiles(path);
                                            ftpClient.disconnect();
                                            if (ftpFiles4.length > 0) {
                                                for (int m = 0; m < ftpFiles4.length; m++) {
                                                    ftpClient = FtpUtil.getClient();
                                                    path = "/" + ftpFiles[i].getName() + "/" + ftpFiles1[j].getName() + "/" + ftpFiles2[k].getName() + "/" + ftpFiles3[l].getName() + "/" + ftpFiles4[m].getName() + "/";
                                                    ftpFiles5 = ftpClient.listFiles(path);
                                                    ftpClient.disconnect();
                                                    if (ftpFiles5.length > 0) {
                                                        for (int n = 0; n < ftpFiles5.length; n++) {
                                                                ftpClient = FtpUtil.getClient();
                                                                path = "/" + ftpFiles[i].getName() + "/" + ftpFiles1[j].getName() + "/" + ftpFiles2[k].getName() + "/" + ftpFiles3[l].getName() + "/" + ftpFiles4[m].getName() + "/" + ftpFiles5[n].getName() + "/";
                                                                ftpFiles6 = ftpClient.listFiles(path);
                                                                if (!ftpFiles6[0].isDirectory()) {
                                                                    String result = "";
                                                                    InputStream ins = ftpClient.retrieveFileStream(path);
                                                                    BufferedReader reader = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
                                                                    String inLine = reader.readLine();
                                                                    while (inLine != null) {
                                                                        result += inLine;
                                                                        inLine = reader.readLine();
                                                                    }
                                                                    ftpClient.disconnect();
                                                                    new MongoWriter().write(path.substring(1, path.length() - 1), ftpFiles5[n].getName(), result);
                                                                    System.out.println("路径：" + path.substring(1, path.length() - 1));
                                                                    System.out.println("文件名：" + ftpFiles5[n].getName());
                                                                    System.out.println("内容：" + result);
                                                                    a++;
                                                                    System.out.println("*************************************" + a);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
    }

